package com.android.c2dm;

import net.arnx.jsonic.JSON;

import org.json.JSONObject;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;

import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.activity.DetailJokesActivity;
import com.fancon.android.gardenjokesx.activity.ProfileActivity;
import com.fancon.android.gardenjokesx.entity.Photo;
import com.fancon.android.gardenjokesx.entity.User;
import com.fancon.android.gardenjokesx.utils.Constant;
import com.fancon.android.gardenjokesx.utils.UserUtil;

public class C2DMMessageReceiver extends BroadcastReceiver {

	private String payload;
	private static int id = 0;
	private String contentTitle = "You have a new message";
	private String contentText = "You have a new message";
	private Notification notification;
	private NotificationManager notificationManager;

	@Override
	public void onReceive(Context context, Intent intent) {
		notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		String action = intent.getAction();
		Log.w("C2DM", "Message Receiver called");
		if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
			Log.w("C2DM", "Received message");
			payload = intent.getStringExtra("payload");
			Log.d("C2DM", "dmControl: payload = " + payload);
			// if(id >= 2){
			// contentTitle = "hihi";
			// contentText = "hehe";
			//
			// }
			// TODO Send this to my application server to get the real data
			// Lets make something visible to show that we received the message
			User user = UserUtil.getInfoUserLogin(context);
			if (user.getUser_id() != null && user.getUser_id().length() > 0) {
				createNotification(context, payload);
				Log.d("id_notify", "" + id);
				sendMessage(context, payload);
			}
		}
	}

	private void sendMessage(Context context, String payload) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor edit = prefs.edit();
		edit.putString("payload", payload);
		edit.commit();
	}

	public void createNotification(Context context, String payload) {
		try {
			JSONObject payloadObj = new JSONObject(payload);
			int ntype = 1;
			String userStr = null;
			String photoStr = null;
			User user = null;
			Photo photo = null;
			if (payloadObj.opt("ntype") != null) {
				ntype = payloadObj.getInt("ntype");
			}
			if (payloadObj.opt("user") != null) {
				userStr = payloadObj.getString("user");
			}
			if (payloadObj.opt("photo") != null) {
				photoStr = payloadObj.getString("photo");
			}
			Intent intent = null;
			try {
				user = JSON.decode(userStr, User.class);
				photo = JSON.decode(photoStr, Photo.class);
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
			contentTitle = context.getString(R.string.xcomic);
			switch (ntype) {
			case Constant.NTYPE_COMMENT:
				contentText = user.getUser_name() + " "
						+ context.getString(R.string.message_comment);
				 intent = new Intent(context, DetailJokesActivity.class);
				 intent.putExtra("photo", photoStr);
				 intent.putExtra(Constant.PHOTO_ID, photo.getPhoto_id());
				 intent.putExtra(Constant.SCREEN_ID, Constant.PUSH_RECEIVE);
				//intent = new Intent(context, U21_22Activity.class);
				break;
			case Constant.NTYPE_FOLLOW:
				contentText = user.getUser_name() + " "
						+ context.getString(R.string.message_follow);
				 intent = new Intent(context, ProfileActivity.class);
				 intent.putExtra(Constant.USER_ID_VIEW, user.getUser_id());
				//intent = new Intent(context, U21_22Activity.class);
				break;
			case Constant.NTYPE_LIKE:
				contentText = user.getUser_name() + " "
						+ context.getString(R.string.message_like);
				photo = JSON.decode(photoStr, Photo.class);
				intent = new Intent(context, DetailJokesActivity.class);
				intent.putExtra("photo", photoStr);
				intent.putExtra(Constant.PHOTO_ID, photo.getPhoto_id());
				intent.putExtra(Constant.SCREEN_ID, Constant.PUSH_RECEIVE);
				break;
			default:
				break;
			}
			notification = new Notification(R.drawable.icon, contentText,
					System.currentTimeMillis());
			// Hide the notification after its selected
			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			id++;
			notification.number = id;

			// intent = new Intent(context, U21_22Activity.class);
			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
					intent, 0);
			notification.setLatestEventInfo(context, contentTitle, contentText,
					pendingIntent);
			notificationManager.notify(0, notification);
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
}